package com.zuihai.app.mvp.ui.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

/**
 * Created by Administrator on 2017/6/5 0005.
 */

public class CareView extends android.support.v7.widget.AppCompatTextView {

    private Paint paint;

    /**
     * 背景颜色
     */
    private int bg_color = 0xffbc7d53;
    private int width;
    private int height;
    private int percent;
    private RectF rectf;
    private int circleAngle;

    public CareView(Context context) {
        super(context);
    }

    public CareView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CareView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(){
        paint = new Paint();
        paint.setStrokeWidth(4);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setColor(bg_color);

        rectf = new RectF();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        circleAngle = height / 2;
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, (width - height) / 2);
        valueAnimator.setDuration(200);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                percent = (int) animation.getAnimatedValue();
            }
        });
        rectf.left = 0;
        rectf.top = 0;
        rectf.right = width;
        rectf.bottom = height;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRoundRect(rectf, circleAngle, circleAngle, paint);
    }
}
